package com.aia.prm.web.ria.app.ui;

import com.vaadin.Application;
import com.vaadin.ui.*;

import java.awt.event.ActionListener;

@SuppressWarnings("serial")
public class AlertDialog extends Window {

	private final HorizontalLayout buttons;

	public static AlertDialog confirmYesNo(Application application, String message, ActionListener onYes) {
		AlertDialog result =  new AlertDialog(message);
		result.addYesButton(onYes);
		result.show(application);
		return result;
	}

	private AlertDialog(String message) {
		setCaption("提示");
		setResizable(true);
		setModal(true);
		setClosable(false);
		setWidth("500px");

		VerticalLayout root = new VerticalLayout();
		root.setSpacing(true);
		root.setMargin(true);
		setContent(root);

		root.addComponent(new Label(message));

		buttons = new HorizontalLayout();
		buttons.setSpacing(true);

		root.addComponent(buttons);
		root.setComponentAlignment(buttons, Alignment.BOTTOM_CENTER);
	}

	private AlertDialog addYesButton(final ActionListener listener) {
		buttons.addComponent(new Button("确定", new Button.ClickListener() {
			public void buttonClick(Button.ClickEvent event) {
				AlertDialog.this.getParent().removeWindow(AlertDialog.this);
				if (listener != null) {
					listener.actionPerformed(null);
				}
			}
		}));
		return this;
	}

	private void show(Application application) {
		application.getMainWindow().addWindow(this);
	}
}
